//
//  AppDelegate.m
//  ZKingAddressBook
//
//  Created by zkingsoft on 15/12/24.
//  Copyright (c) 2015年 zkingsoft. All rights reserved.
//

#import "AppDelegate.h"

#import "ZKingAddressBookViewController.h"
#import "ZKingGetGoodsViewController.h"
#import "ZKingSendGoodsViewController.h"
#import "ZKingPersonCenterViewController.h"


@interface AppDelegate ()<UITabBarControllerDelegate>{
    UITabBarController *_tabBarController;
    UIView *_backView;
    
    NSInteger _selectIndex;
    UIView *_selectBackView;
}

@property (nonatomic, retain) UITabBarController        *tabBarController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // 添加tabbar上的四个视图控制器
    ZKingAddressBookViewController *addressBookViewController = [[ZKingAddressBookViewController alloc] init];
    UINavigationController *addressBookNav = [[UINavigationController alloc] initWithRootViewController:addressBookViewController];
    
    
    
    ZKingSendGoodsViewController *sendGoodsViewController = [[ZKingSendGoodsViewController alloc] init];
    UINavigationController *sendGoodsNav = [[UINavigationController alloc] initWithRootViewController:sendGoodsViewController];
    
    
    
    ZKingGetGoodsViewController *getGoodsViewController = [[ZKingGetGoodsViewController alloc] init];
    UINavigationController *getGoodsNav = [[UINavigationController alloc] initWithRootViewController:getGoodsViewController];
    
    
    ZKingPersonCenterViewController *personCenterViewController = [[ZKingPersonCenterViewController alloc] init];
    UINavigationController *personCenterNav = [[UINavigationController alloc] initWithRootViewController:personCenterViewController];
    
    
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:addressBookNav, getGoodsNav, sendGoodsNav, personCenterNav, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    
    
    // 对tabBar上的视图显示内容进行修改
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"物信" image:[UIImage imageNamed:@"ic_order_g.png"] selectedImage:[UIImage imageNamed:@"ic_order_w.png"]];
    addressBookViewController.tabBarItem = item1;
    
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"发货" image:[UIImage imageNamed:@"ic_home_g.png"] selectedImage:[UIImage imageNamed:@"ic_home_w.png"]];
    sendGoodsViewController.tabBarItem = item2;
    
    UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"收货" image:[UIImage imageNamed:@"ic_changshi_g.png"] selectedImage:[UIImage imageNamed:@"ic_changshi_w.png"]];
    getGoodsViewController.tabBarItem = item3;
    
    UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"个人中心" image:[UIImage imageNamed:@"ic_me_green.png"] selectedImage:[UIImage imageNamed:@"ic_me_w.png"]];
    personCenterViewController.tabBarItem = item4;
    
    // 设置tabbar和选中的tabbar的背景颜色
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 49)];
    _backView.backgroundColor = [UIColor colorWithRed:55/255.0 green:66/255.0 blue:100/255.0 alpha:1.0];
    [self.tabBarController.tabBar insertSubview:_backView atIndex:0];
    self.tabBarController.tabBar.opaque = YES;
    
    _selectIndex = self.tabBarController.selectedIndex;
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    

    _selectBackView = [[UIView alloc] initWithFrame:CGRectMake(width/4*_selectIndex, 0, width/4, 49)];
    _selectBackView.backgroundColor = [UIColor colorWithRed:220/255.0 green:55/255.0 blue:65/255.0 alpha:1.0];
    [self.tabBarController.tabBar insertSubview:_selectBackView atIndex:1];
    self.tabBarController.tabBar.opaque = YES;

    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//    [_selectBackView removeFromSuperview];
    _selectIndex = self.tabBarController.selectedIndex;
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    
    _selectBackView.frame = CGRectMake(width/4*_selectIndex, 0, width/4, 49);
    _selectBackView.backgroundColor = [UIColor colorWithRed:220/255.0 green:55/255.0 blue:65/255.0 alpha:1.0];
    [self.tabBarController.tabBar insertSubview:_selectBackView aboveSubview:_backView];
    self.tabBarController.tabBar.opaque = YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
